package flc.ast.adapter;

import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.SimpleDateFormat;

import flc.ast.R;
import flc.ast.bean.MyVideoBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.utils.TimeUtil;

public class VideoAdapter extends StkProviderMultiAdapter<MyVideoBean> {

    public VideoAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(100));
        addItemProvider(new VideoProvider());
    }

    private boolean isEdit = false;
    private boolean isSel=false;

    public void setSel(boolean sel) {
        isSel = sel;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private String text = "";

    public void setText(String text) {
        this.text = text;
    }

    private class VideoProvider extends BaseItemProvider<MyVideoBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_video;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyVideoBean bean) {
            Glide.with(getContext()).load(bean.getPath()).into((ImageView) baseViewHolder.getView(R.id.ivVideoItemImg));
            if (TextUtils.isEmpty(text)) {
                baseViewHolder.setText(R.id.tvVideoItemName, bean.getName());
            } else {
                int index = bean.getName().indexOf(text);
                int len = text.length();
                Spanned temp = Html.fromHtml(bean.getName().substring(0, index) + "<font color=#3160FF>" + bean.getName().substring(index, index + len) + "</font>" + bean.getName().substring(index + len, bean.getName().length()));
                baseViewHolder.setText(R.id.tvVideoItemName, temp);
            }
            baseViewHolder.setText(R.id.tvVideoItemSize,ConvertUtils.byte2FitMemorySize(bean.getSize()));
            baseViewHolder.setText(R.id.tvVideoItemDate, TimeUtils.millis2String(FileUtils.getFileLastModified(bean.getPath()), new SimpleDateFormat("yyyy-MM-dd")));
            baseViewHolder.setText(R.id.tvVideoItemLength, TimeUtil.getHHmmss(bean.getLength()));
            if (isEdit) {
                baseViewHolder.getView(R.id.ivVideoItemSel).setVisibility(View.VISIBLE);
                if (bean.isSelect()) {
                    baseViewHolder.setImageResource(R.id.ivVideoItemSel, R.drawable.axuanzhong);
                } else {
                    baseViewHolder.setImageResource(R.id.ivVideoItemSel, R.drawable.aweixuanzhong);
                }
            } else {
                baseViewHolder.getView(R.id.ivVideoItemSel).setVisibility(View.INVISIBLE);
            }
            if (isSel){
                baseViewHolder.getView(R.id.ivVideoItemMore).setVisibility(View.GONE);
            }else {
                baseViewHolder.getView(R.id.ivVideoItemMore).setVisibility(View.VISIBLE);
            }
        }
    }
}
